home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSQUERY.C < prev    next >
C/C++ Source or Header  |  1993-06-03  |  5KB  |  147 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsquery.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to remotely verify a user code. The system works
  25. //    like this:
  26. //        1) Customer software generates a code
  27. //        2) Customer calls with code and is given response
  28. //        3) Customer enters response into software and it is verified before
  29. //            proceding.
  30. //
  31. //    The code is an 8 character alphanumeric sequence.
  32. //
  33. //    The code in this module should be written entirely in C. 
  34. //    Do not use any C++ constructs.
  35. //
  36. //    This module is portable to:
  37. //        DOS 3.X+
  38. //        MS Windows 3.X+
  39. //        OS/2 2.X+
  40. //        OS/2 2.0 PM
  41. //        SCO UNIX.
  42. //
  43. //    The following compilers are supported:
  44. //        MSC 6.0A
  45. //        MSC/C++ 7.0
  46. //        Borland C++ 3.1 for DOS
  47. //        Borland C++ 1.0 for OS/2 2.X
  48. //        SCO UNIX cc
  49. //
  50. //----------------------------------------------------------------------------
  51. #include <bs.h>
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Generate a random query code.
  56. //    Parameters:    psz            9 byte buffer to recieve code.
  57. //       Returns:    TRUE if successful.
  58. //----------------------------------------------------------------------------
  59. VOID FN_E QueryGenerate(PSZ psz)
  60. {
  61.     ULONG ul = Random(0, 10000);            // Get a random number
  62.     ul *= (ULONG)Pid();                        // Multiply by process id
  63.     ul *= (ULONG)time(NULL);                // Multiply by time
  64.     sprintf(psz, "%08lX", ul);                // Print to buffer as hex
  65.     return ;
  66. }
  67.  
  68.  
  69. //----------------------------------------------------------------------------
  70. //   Description:    Generate a random query code.
  71. //    Parameters:    psz            9 byte buffer containing code.
  72. //                                        On return, the buffer contains the response.
  73. //                        pcszKey        User defined key unqiue to this application.
  74. //       Returns:    
  75. //----------------------------------------------------------------------------
  76. VOID FN_E QueryResponse(PSZ psz, PCSZ pcszKey)
  77. {
  78.     CRC crc;
  79.  
  80.     Assert(psz && pcszKey);
  81.     crc = CrcCalcAppend((PBYTE)psz, strlen(psz)+1, (CRC)0);
  82.     crc = CrcCalcAppend((PBYTE)pcszKey, strlen(pcszKey)+1, crc);
  83.     sprintf(psz, "%08lX", crc);
  84.     return ;
  85. }
  86.  
  87.  
  88. //----------------------------------------------------------------------------
  89. //   Description:    Verify a random query code.
  90. //    Parameters:    pcsz                9 byte buffer containing code
  91. //                        pcszResponse    9 byte buffer to recieve code.
  92. //                        pcszKey            User defined key unqiue to this application.
  93. //       Returns:    TRUE if code was verified
  94. //----------------------------------------------------------------------------
  95. BOOL FN_E QueryVerify(PCSZ pcsz, PCSZ pcszResponse, PCSZ pcszKey)
  96. {
  97.     CHAR szResponse[9];
  98.  
  99.     strcpy(szResponse, pcsz);
  100.     QueryResponse(szResponse, pcszKey);
  101.     //
  102.     //    This is a programmer back door for testing.
  103.     //    The response should allow the sum of the hex value of the
  104.     //    to strings to equal 0xDEADDEAD.
  105.     //
  106. #if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS || OS_OS2)
  107.     if (DebugMode())
  108.         {
  109.         if (strcmpi(pcszResponse, "DEADDEAD") == 0)
  110.             return TRUE;
  111.         }
  112. #endif
  113.     return stricmp(szResponse, pcszResponse) == 0;
  114. }
  115.  
  116.  
  117. //----------------------------------------------------------------------------
  118. //   Description:    Run standard test suite
  119. //    Parameters:    sTest        Test to run.
  120. //                                        0        Run all default tests (except).
  121. //       Returns:    TRUE if successful.
  122. //----------------------------------------------------------------------------
  123. #if COMPILE_TEST
  124. BOOL FN QueryTest(SHORT sTest)
  125. {
  126. static PCSZ pcszKey = "This is a test";
  127.     CHAR szCode[9], szResponse[9];
  128.  
  129.     NOTUSED(sTest);
  130.     QueryGenerate(szCode);
  131.     strcpy(szResponse, szCode);
  132.     QueryResponse(szResponse, pcszKey);
  133.     Output(
  134.         "    Code: %s\n"
  135.         "Response: %s\n"
  136.         "Verified: %s\n",
  137.         szCode, szResponse,
  138.         (QueryVerify(szCode, szResponse, pcszKey) ? "YES": "NO"));
  139.  
  140.     return TRUE;
  141. }
  142. #endif
  143. //----------------------------------------------------------------------------
  144. //------------------------------- End of File --------------------------------
  145. //----------------------------------------------------------------------------
  146.  
  147.